package com.shiro.request;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest;

import com.shiro.utils.Constant;


public class OAuthResourceClientRequest extends OAuthBearerClientRequest{

	private String uri = null;
	

	  public OAuthResourceClientRequest( String url) {
	    super(url);
	   
	  }

	  public OAuthResourceClientRequest setParameter(String paramName, String paramValue) {
	    this.parameters.put(paramName, paramValue);
	    return this;
	  }

	  public OAuthResourceClientRequest setOpentID(String opentID) {
	    this.parameters.put(Constant.HTTP_REQUEST_PARAM_OPEN_ID, opentID);
	    return this;
	  }

	  public String getOpenID() {
	    return this.parameters.get(Constant.HTTP_REQUEST_PARAM_OPEN_ID).toString();
	  }

	  public OAuthResourceClientRequest setClientID(String clientID) {
	    this.parameters.put(Constant.HTTP_REQUEST_PARAM_CLIENT_ID, clientID);
	    return this;
	  }

	  public String getClientID() {
	    return this.parameters.get(Constant.HTTP_REQUEST_PARAM_CLIENT_ID).toString();
	  }
	  public String getUri() {
	    return this.uri;
	  }

	  public void setUri(String uri) {
	    this.uri = uri;
	  }

	  public String toString()
	  {
	    Set keySet = this.parameters.keySet();
	    List<String> paramkeys = new ArrayList<String>(keySet);
	    Collections.sort(paramkeys);

	    Map newParams = new HashMap(this.parameters);

	    StringBuilder paramBuilder = new StringBuilder();
	    for (String key : paramkeys) {
	      String value = newParams.get(key).toString();
	      if ((value != null) && (!"".equals(value))) {
	        paramBuilder.append(key).append("=").append(value).append("&");
	      }
	    }
	    paramBuilder.deleteCharAt(paramBuilder.length() - 1);
	    String param = paramBuilder.toString();

	    paramBuilder = new StringBuilder()
	      .append(encode(param));

	    return paramBuilder.toString();
	  }

	  public String toString1()
	  {
	    Set keySet = this.parameters.keySet();
	    List<String> paramkeys = new ArrayList<String>(keySet);
	    Collections.sort(paramkeys);

	    Map newParams = new HashMap(this.parameters);

	    StringBuilder paramBuilder = new StringBuilder();
	    for (String key : paramkeys) {
	      String value = newParams.get(key).toString();
	      if ((value != null) && (!"".equals(value))) {
	        paramBuilder.append(key).append("=").append(value).append("&");
	      }
	    }
	    paramBuilder.deleteCharAt(paramBuilder.length() - 1);
	    String param = paramBuilder.toString();
	    paramBuilder = new StringBuilder(encode(param));
	    return paramBuilder.toString();
	  }

	  private static String encode(String src) {
	    try {
	      return URLEncoder.encode(src, "UTF-8");
	    } catch (UnsupportedEncodingException e) {
	      e.printStackTrace();
	    }return src;
	  }
}
